//全局等待效果
var init_waiting={
    view:function(){
        return m("div",{
            style:"width:160px; height:40px; position:fixed; top:50%; left:50%; margin-left:-80px; margin-top:-20px; background:white; text-align:center; line-height:40px; box-shadow:0 0 5px #999999; border:solid 1px silver; user-select:none;"
        },init.msg)
    }
}
//全局错误信息效果
var init_error={
    view:function(){
        return m("div",{
            style:"width:300px; position:fixed; top:20%; left:50%; margin-left:-150px; background:white; text-align:center; line-height:40px; box-shadow:0 0 5px #999999; border:solid 1px silver; user-select:none;"
        },[
            m("div",{
                style:"padding:0 20px; height:30px; line-height:30px; background:#e2e2e2; color:red;"
            },"错误提示"),
            m("div",{
                style:"padding:10px 20px; line-height:30px; text-align:left;"
            },init.msg),
            m("div",{
                style:"width:100%; height:30px; display:flex;"
            },[
                m("button",{
                    style:"border:0; width:100%; height:30px; cursor:pointer; background:#f2f2f2; flex:1;",
                    onmouseover:function(){ this.style.background="#e2e2e2";},
                    onmouseout:function(){ this.style.background="#f2f2f2";},
                    onclick:function(){
                        init.Hide();
                        if(typeof init.callback!="undefined"){
                            init.callback();
                        }
                    }
                },"取消")
            ])
        ])
    }
}
//全局确认操作
var init_confirm={
    view:function(){
        return m("div",{
            style:"width:300px; position:fixed; top:20%; left:50%; margin-left:-150px; background:white; text-align:center; line-height:40px; box-shadow:0 0 5px #999999; border:solid 1px silver; user-select:none;"
        },[
            m("div",{
                style:"padding:0 20px; height:30px; line-height:30px; background:#e2e2e2; color:blue;"
            },"确定操作"),
            m("div",{
                style:"padding:10px 20px; line-height:30px; text-align:left;"
            },init.msg),
            m("div",{
                style:"width:100%; height:30px; display:flex;"
            },[
                m("button",{
                    style:"border:0; width:100%; height:30px; cursor:pointer; background:#a2a2a2; flex:1;",
                    onmouseover:function(){ this.style.background="#e2e2e2";},
                    onmouseout:function(){ this.style.background="#a2a2a2";},
                    onclick:function(){
                        init.Hide();
                        init.callback();
                    }
                },"确定"),
                m("button",{
                    style:"border:0; width:100%; height:30px; cursor:pointer; background:#f2f2f2; flex:1;",
                    onmouseover:function(){ this.style.background="#e2e2e2";},
                    onmouseout:function(){ this.style.background="#f2f2f2";},
                    onclick:function(){
                        init.Hide();
                    }
                },"取消")
            ])
        ])
    }
}
//页面初始化
var init={
    msg:"",//全局信息
    callback:function(){},//回调函数
    //验证浏览器，mithril不支持IE10
    CheckBrower:function(){
        var userAgent = navigator.userAgent;
        // console.log(userAgent);
        if(userAgent.indexOf("Opera")>-1){
            console.log("Opera");
            return true
        }
        if(userAgent.indexOf("Firefox")>-1){
            console.log("Firefox");
            return true
        }
        if(userAgent.indexOf("Chrome")>-1){
            console.log("Chrome");
            return true
        }
        if(userAgent.indexOf("Edge")>-1){
            console.log("Edge");
            return true
        }
        if(userAgent.indexOf("Safari")>-1){
            console.log("Safari");
            return true
        }
        if(userAgent.indexOf("MSIE")>-1){
            console.log("MSIE");
            document.write("<h1>IE10以下版本不支持该网站</h1>");
            return false
        }
        return true
    },
    //初始化initbg
    Show:function(option={method:"Waiting",url:"",msg:""},callback){
        init.msg="";
        init.url="";
        if(!document.querySelector(".initbg")){
            var page=document.createElement("div");
            page.setAttribute("class","initbg");
            page.setAttribute("style","width:100%; height:100vh; background:rgba(80,80,80,0.1); position:fixed; top:0; left:0;display:none;");
            document.body.appendChild(page);
        }
        document.querySelector(".initbg").style.display="block";

        init.msg=typeof option.msg=="undefined"?"发生错误，无提示信息":option.msg;
        
        init.callback=typeof callback!="undefined"?callback:function(){};
        //方法选择
        if(option.method=="Waiting"){
            init.msg="初始化，请等待";
            m.render(document.body,m(init_waiting));
            return;
        }
        if(option.method=="Submit"){
            init.msg="提交中，请等待";
            m.render(document.querySelector(".initbg"),m(init_waiting));
            return;
        }
        if(option.method=="Error"){
            m.render(document.querySelector(".initbg"),m(init_error));
            return;
        }
        if(option.method=="Confirm"){
            m.render(document.querySelector(".initbg"),m(init_confirm));
            return;
        }
    },
    //隐藏initbg
    Hide:function(){
        // init.callback=function(){};
        document.querySelector(".initbg").style.display="none";
        m.render(document.querySelector(".initbg"),"");
    },
    //回调渲染页面
    loading:function(callback){
        if(!init.CheckBrower()){
            return;
        }
        //回调函数
        try{
            init.Show();
            callback();
        }catch (e) {
            document.write("<h1>你的浏览器出现异常，请尝试使用Chrome浏览器</h1>");
            console.log(e.stack);
        }
    }
}